package com.sinothk.gpserver.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.gpserver.modules.system.entity.UserRoleEntity;
import com.sinothk.gpserver.modules.system.service.IUserRoleService;
import com.sinothk.gpserver.util.DateUtils;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * a用户角色功能
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-08-27
 */
@Api(tags = "用户角色功能")
@RequestMapping("/userRole")
@ResponseResult()
@RestController
public class UserRoleController extends BaseController {

    @Autowired
    IUserRoleService userRoleService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<UserRoleEntity> page(@RequestBody PageVo<UserRoleEntity> pageVo) {
        return userRoleService.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public UserRoleEntity get(@ApiParam("id") @PathVariable String id) {
        return userRoleService.getById(id);
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody UserRoleEntity userRoleEntity) {
        if (StringUtils.isBlank(userRoleEntity.getUserAccount())) throw new BizException("userAccount不能为空");
        if (userRoleEntity.getRoles() == null) throw new BizException("role不能为空");

        LambdaQueryWrapper<UserRoleEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRoleEntity::getUserAccount, userRoleEntity.getUserAccount());
        List<UserRoleEntity> list = userRoleService.list(wrapper);
        if (list != null && list.size() > 1) {
            throw new BizException("错误(一个账号有多条数据)");
        }

        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (list != null && list.size() == 1) {
            UserRoleEntity oldUserRole = list.get(0);
            oldUserRole.setRoles(userRoleEntity.getRoles());
            oldUserRole.setUpdateBy(login.getUserName());
            oldUserRole.setUpdateTime(DateUtils.getLocalDateTime());
            userRoleService.updateById(oldUserRole);
            return "更新成功";
        } else {
            userRoleEntity.setCreateBy(login.getAccount());
            userRoleEntity.setCreateTime(DateUtils.getLocalDateTime());
            userRoleService.save(userRoleEntity);
            return "新增成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        userRoleService.removeById(id);
        return "已逻辑删除";
    }
}
